package com.kitbox.hl.meshow.common.aop;

import com.kitbox.hl.meshow.common.annotation.TimeAnnotation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;

/**
 * @Auther: honglei
 * @Date: 2019/8/22 15:22
 * @Description:
 */
public class TimeAspect {
    private static final Log logger = LogFactory.getLog(TimeAspect.class);

    @Around(value = "@annotation(time)")
    public Object logTime(ProceedingJoinPoint pjd, TimeAnnotation time) throws Throwable {
        // 获取目标方法签名
        String signature = pjd.getSignature().toString();
        long now = System.currentTimeMillis();
        Object result = pjd.proceed();
        long consumeTime = System.currentTimeMillis() - now;
        if (consumeTime >= time.overtime()) {
            logger.info("执行" + signature + "消耗的时间为" + consumeTime + "毫秒");
        }
        return result;
    }
}
